package com.huahang.fashionpress.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huahang.fashionpress.dataobject.PageOrderMasterList;
import com.huahang.fashionpress.model.Order;
import com.huahang.fashionpress.service.OrderService;
import com.huahang.fashionpress.vo.OrderDetailVO;
import com.huahang.fashionpress.vo.OrderMasterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.naming.event.NamingExceptionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 后台订单控制器
 * @Author: 阿俊哥
 * @Date: 2019/8/26 22:15
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class AdminOrderController {

    @Resource
    OrderService orderService;

    /**
     * 显示订单主列表 默认显示未隐藏的页面
     * @param map
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView getOrderMasterList(  @RequestParam(name = "size",defaultValue = "10") int size,
                                             @RequestParam(name = "pagenum",defaultValue = "1") int pagenum,
                                                Map<String,Object> map){
       PageOrderMasterList pageOrderMasterList = orderService.selectOrderList(size,pagenum);
       map.put("pagenum", pageOrderMasterList.getPageNum());
       map.put("totalPage", pageOrderMasterList.getTotalPage());
        map.put("orderMasterVOList", pageOrderMasterList.getOrderMasterVOList());
        return new ModelAndView("/admin/order/list", map);

    }

    /**
     * 修改订单的状态 包括未发货到发货状态 退款中到订单完成状态
     * @param orderId
     * @param orderStatus
     * @return
     */
    @RequestMapping(value = "/updateOrderStatus/{orderId}/{orderStatus}",method = RequestMethod.GET)
    public String updateStatus(@PathVariable String orderId,@PathVariable String orderStatus){
        int orderStatus1 = Integer.parseInt(orderStatus);
        boolean flag = orderService.updateOrderStatus(orderId, orderStatus1);
        if(flag){
            return "操作成功！";
        }
        return "操作失败！";
    }

    /**
     * 查询订单详情
     * @param orderId
     * @return
     * @throws JsonProcessingException
     */
//    @RequestMapping(value = "/orderDetailList/{orderId}",method = RequestMethod.GET)
//    public String getOrderDetailList(@PathVariable String orderId) throws JsonProcessingException {
//        List<OrderDetailVO> orderDetailVOList = orderService.selectOrderDetails(orderId);
//        //返回json
//        ObjectMapper objectMapper = new ObjectMapper();
//        return objectMapper.writeValueAsString(orderDetailVOList);
//
//    }

    /**
     *
     */
    @RequestMapping(value = "/selectOrderById/{orderId}",method = RequestMethod.GET)
    public ModelAndView selectOrderById(@PathVariable String orderId,Map<String,Object> map){
        if(orderId.equals("")||orderId==null){
            log.info("【订单管理】根据id查询订单出错，错误参数orderId="+orderId);
            map.put("msg", "输入的编号错误");
            map.put("url", "/order/list");
            return new ModelAndView("redirect:/admin/common/error", map);
        }
        OrderMasterVO order = orderService.selectOrderById(orderId);
        if(order==null){
            log.info("【订单管理】根据id查询订单出错，未找到该订单");
            map.put("msg", "未找到该订单");
            map.put("url", "/order/list");
            return new ModelAndView("redirect:/admin/common/error", map);
        }
        List<OrderMasterVO> orderMasterVOList = new ArrayList<>();
        orderMasterVOList.add(order);
        map.put("pagenum", 1);
        map.put("totalPage", 1);
        map.put("orderMasterVOList", orderMasterVOList);
        return new ModelAndView("admin/order/list", map);

    }

}
